﻿package me.zhengjie

import me.zhengjie.logging.slf4j.logInfo
import me.zhengjie.modules.system.domain.User
import me.zhengjie.modules.system.repository.UserRepository
import me.zhengjie.modules.system.rest.UserController
import me.zhengjie.modules.system.service.UserService
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.ComponentScan
import org.springframework.scheduling.annotation.EnableAsync

@EnableAsync
@ComponentScan(value = ["me.zhengjie"])
//@TestPropertySource(locations = ["classpath:application-test.yml"])
//@EnableWebMvc

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class UsersTest {
    @Autowired
    private val userRepository: UserRepository? = null

    @Autowired
    private val userService: UserService? = null

    //@Autowired
    //private val userMapper: UserMapper? = null

    @Autowired
    private val userController: UserController? = null

    /**
     * 测试user对象映射
     */
    @Test
    fun userObjectTest() {
        try {

            val user: User? = userRepository!!.findByUsername("admin")
            logInfo("用户名为：${user?.username}")

            // 访问 roles 集合
            user?.roles?.forEach { role ->
                println(role.name)
            }
            logInfo("roles count ${user?.roles?.size}")
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {

        }
    }
    @Test
    fun jwtUserTest() {
        try {
            val jwtUser = JwtUser(
                id = 1,
                username = "admin",
                password = "admin",
                authorities = listOf(),
                enabled = true
            )


            println(jwtUser.getUsername())
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}